package DFS;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-19
 * Time:16:35
 * Text：814. 二叉树剪枝
 */
public class Text2 {
    public TreeNode dfs(TreeNode root){
        if(root.left!=null) root.left=dfs(root.left);
        if(root.right!=null) root.right=dfs(root.right);

        if(root.left==null && root.right==null){
            if(root.val==0) return null;
        }return root;
    }
    public TreeNode pruneTree(TreeNode root) {
        return dfs(root);
    }

    public static void main(String[] args) {
        TreeNode t1=new TreeNode(0);
        TreeNode t3=new TreeNode(0);
        TreeNode t4=new TreeNode(0);
        TreeNode t5=new TreeNode(0);
        t1.right=t3;
        t3.left=t4;
        t3.right=t5;
        Text2 t=new Text2();
        t.pruneTree(t1);
    }
}
